//--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
// Copyright 2012 Microsoft Corporation. All Rights Reserved
// Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License.   
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
// Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
//--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

namespace Preflight.ServiceModel.Client
{
    using System;
    using Security;

    public class StandardEndpointFactory
    {
        public EndpointDescription CreateAcsSecuredServiceDescription(string name, Uri serviceBaseUri, Uri acsServiceUri, Uri realmOrScope, string username, string password)
        {
            if (acsServiceUri == null)
            {
                throw new ArgumentNullException("acsServiceUri");
            }

            if (realmOrScope == null)
            {
                throw new ArgumentNullException("realmOrScope");
            }

            if (string.IsNullOrWhiteSpace(username))
            {
                throw new ArgumentNullException("username");
            }

            if (string.IsNullOrWhiteSpace(password))
            {
                throw new ArgumentNullException("password");
            }

            var description = CreateDescription(name, StandardEndpointTypes.AcsAuthWithUsernamePassword, serviceBaseUri);
            description.Properties[CommonPropertyNames.AcsEndpoint] = acsServiceUri.ToString();
            description.Properties[CommonPropertyNames.AcsRealm] = realmOrScope.ToString();
            description.Properties[CommonPropertyNames.Username] = username;
            description.Properties[CommonPropertyNames.Password] = password;
            return description;
        }

        public EndpointDescription CreateUnsecureHttpDescription(string name, Uri serviceBaseUri)
        {
            var description = CreateDescription(name, StandardEndpointTypes.UnsecureHttpEndpoint, serviceBaseUri);
            return description;
        }

        public EndpointDescription CreateSecureHttpDescription(string name, Uri serviceBaseUri, CertificateLocation certificateLocation)
        {
            if (certificateLocation == null)
            {
                throw new ArgumentNullException("certificateLocation");
            }

            var description = CreateDescription(name, StandardEndpointTypes.SecureHttpEndpoint, serviceBaseUri);
            description.Properties[CommonPropertyNames.CertificateLocation] = certificateLocation.ToString();
            return description;
        }

        public EndpointDescription CreateWindowsAuthenticationEndpointDescription(string name, Uri serviceBaseUri)
        {
            var description = CreateDescription(name, StandardEndpointTypes.WindowsAuthHttpEndpoint, serviceBaseUri);
            return description;
        }

        private static EndpointDescription CreateDescription(string name, string type, Uri serviceBaseUri)
        {
            if (string.IsNullOrWhiteSpace(name))
            {
                throw new ArgumentNullException("name");
            }

            if (serviceBaseUri == null)
            {
                throw new ArgumentNullException("serviceBaseUri");
            }

            var description = new EndpointDescription
                {
                    Name = name,
                    Type = type
                };

            description.Properties[CommonPropertyNames.ServiceUri] = serviceBaseUri.ToString();

            return description;
        }
    }
}
